package Thread;

import jdk.nashorn.internal.ir.Block;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo30 {
    public static void main(String[] args) {
        //BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(1000);
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(1000);

        //消费者
        Thread t1 = new Thread(() -> {
            try{
                while(true){
                    Integer value = queue.take();
                    System.out.println("t1 消费: " + value);
                    //每隔1s消费一个元素
                    Thread.sleep(1000);
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            
        });
        
        //生产者
        Thread t2 = new Thread(() -> {
            try
            {
                int count = 1;
                while(true){
                    queue.put(count);
                    System.out.println("t2 生产: "+count);
                    count++;
                    //每隔1s生产一个元素
                    //Thread.sleep(1000);
                }
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        });

        //生产者
        Thread t3 = new Thread(() -> {
            try
            {
                int count = 1;
                while(true){
                    queue.put(count);
                    System.out.println("t3 生产: "+count);
                    count++;
                    //每隔1s生产一个元素
                    //Thread.sleep(1000);
                }
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        });

        t1.start();
        t2.start();
        //t3.start();

    }
}
